<!-- subpkg_archive/list/index.vue -->
<script setup>
  import { ref } from 'vue'
  import { patientListApi } from '../../apis/user'
  import { removePatientApi } from '../../apis/patient'
  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
      },
    },
  ])
  // 患者列表
  const patinetList = ref([])
  const patientListData = async () => {
    const res = await patientListApi()
    // console.log(res)
    patinetList.value = res.data
  }
  patientListData()

  // 滑动操作点击
  async function onSwipeActionClick(id, index) {
    // 传递数据的 id 值和索引值

    // 显示删除确认对话框
    wx.showModal({
      title: '确认删除',
      content: '您确定要删除患者吗吗？',
      success: async (res) => {
        if (res.confirm) {
          // 用户点击了确认按钮
          await removePatientApi(id)
          // 更新患者列表数据
          this.patientListData()
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 2000, // 持续时间为2秒
          })
        } else if (res.cancel) {
          // 用户点击了取消按钮
          // console.log('用户取消了删除操作');
        }
      },
    })
  }
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          :right-options="swipeOptions"
          v-for="(item, index) in patinetList"
          :key="item.id"
          @click="onSwipeActionClick(item.id, index)"
        >
          <view
            class="archive-card"
            :class="{ active: item.defaultFlag === 1 }"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{
                item.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2')
              }}</text>
              <text class="default" v-if="item.defaultFlag === 1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/index?id=${item.id}`"
            >
              <uni-icons
                type="icon-edit"
                size="20"
                color="#16C2A3"
                custom-prefix="iconfont"
              />
            </navigator>
          </view>
        </uni-swipe-action-item>

        <!-- <uni-swipe-action-item :right-options="swipeOptions">
          <view class="archive-card">
            <view class="archive-info">
              <text class="name">李富贵</text>
              <text class="id-card">321***********6164</text>
            </view>
            <view class="archive-info">
              <text class="gender">男</text>
              <text class="age">32岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              url="/subpkg_archive/form/index"
            >
              <uni-icons
                type="icon-edit"
                size="20"
                color="#16C2A3"
                custom-prefix="iconfont"
              />
            </navigator>
          </view>
        </uni-swipe-action-item> -->
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patinetList.length < 6" class="archive-card">
        <navigator
          class="add-link"
          hover-class="none"
          url="/subpkg_archive/form/index"
        >
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
    </view>
  </scroll-page>
</template>

<style lang="scss">
  @import './index.scss';
</style>
